#!/usr/bin/perl

use strict;
use Card;
use DefaultDeck;

my $wins = 0;
my $losses = 0;
my $continue = 1;
print "\n";
while ($continue == 1) {
	if (play()) {
		$wins++;
		print "You win.\n";
	} else {
		$losses++;
		print "You lose.\n";
	}
	print "You have won $wins game(s) and lost $losses game(s).\n";
	print "Would you like to continue playing (1) or stop (0)?\n";
	$continue = <STDIN>;
	while ($continue*$continue != $continue) {
		print "Please respond with 0 or 1.\n";
		$continue = <STDIN>;
	}
	print "\n";
}
print "Your final score is $wins win(s) and $losses loss(es).\n";

sub play {
	my $myfirst = new Card();
	my $myfirstname;
	my $myfirstvalue;
	my $mysecond = new Card();
	my $mysecondname;
	my $mysecondvalue;
	my $dealerfirst = new Card();
	my $dealerfirstname;
	my $dealerfirstvalue;
	my $dealersecond = new Card();
	my $dealersecondname;
	my $dealersecondvalue;
	my $myscore;
	my $dealerscore;
	my $deck = new DefaultDeck();
	my $myaces;
	my $dealeraces;
	
	$myfirst->setname($deck->draw()->getname());
	$myfirstname = $myfirst->getname();
	$myfirstvalue = $myfirst->getvalue();
	if ($myfirstvalue == 1) {
		$myfirstvalue = 11;
		$myaces++;
	} elsif ($myfirstvalue == 11 || $myfirstvalue == 12 || $myfirstvalue == 13) {
		$myfirstvalue = 10;
	}
	print "You drew $myfirstname with value $myfirstvalue.\n";
	
	$dealerfirst->setname($deck->draw()->getname());
	$dealerfirstname = $dealerfirst->getname();
	$dealerfirstvalue = $dealerfirst->getvalue();
	if ($dealerfirstvalue == 1) {
		$dealerfirstvalue = 11;
		$dealeraces++;
	} elsif ($dealerfirstvalue == 11 || $dealerfirstvalue == 12 || $dealerfirstvalue == 13) {
		$dealerfirstvalue = 10;
	}
	print "The dealer drew a card.\n";

	$mysecond->setname($deck->draw()->getname());
	$mysecondname = $mysecond->getname();
	$mysecondvalue = $mysecond->getvalue();
	if ($mysecondvalue == 1) {
		$mysecondvalue = 11;
		$myaces++;
	} elsif ($mysecondvalue == 11 || $mysecondvalue == 12 || $mysecondvalue == 13) {
		$mysecondvalue = 10;
	}
	print "You drew $mysecondname with value $mysecondvalue.\n";
	
	$dealersecond->setname($deck->draw()->getname());
	$dealersecondname = $dealersecond->getname();
	$dealersecondvalue = $dealersecond->getvalue();
	if ($dealersecondvalue == 1) {
		$dealersecondvalue = 11;
		$dealeraces++;
	} elsif ($dealersecondvalue == 11 || $dealersecondvalue == 12 || $dealersecondvalue == 13) {
		$dealersecondvalue = 10;
	}
	print "The dealer drew $dealersecondname with value $dealersecondvalue.\n\n";
	
	$myscore = $myfirstvalue + $mysecondvalue;
	my $myinput = 1;
	while ($myinput == 1) {

		if ($myscore > 21) {
			if ($myaces > 0) {
				$myaces--;
				$myscore -= 10;
			} else {
				print "Your current score is $myscore.\n";
				print "You have gone over 21.\n\n";
				last;
			}
		}

		print "Your current score is $myscore.\n";

		print "What would you like to do? Hit (1) or Stay (0)?\n";
		$myinput = <STDIN>;
		while ($myinput*$myinput != $myinput) {
			print "Please respond with 0 or 1.\n";
			$myinput = <STDIN>;
		}
		print "\n";	
		if ($myinput == 1) {
			my $mynext = new Card();
			$mynext->setname($deck->draw()->getname());
			my $mynextname = $mynext->getname();
			my $mynextvalue = $mynext->getvalue();
			if ($mynextvalue == 1) {
				$myaces++;
				$mynextvalue = 11;
			} elsif ($mynextvalue == 11 || $mynextvalue == 12 || $mynextvalue == 13) {
				$mynextvalue = 10;
			}
			print "You drew $mynextname with value $mynextvalue.\n";
			$myscore += $mynextvalue;
		}
	}

	$dealerscore = $dealerfirstvalue + $dealersecondvalue;
	my $dealerinput = 1;
	while ($dealerinput == 1) {

		if ($dealerscore > 21) {
			if ($dealeraces > 0) {
				$dealeraces--;
				$dealerscore -= 10;
			} else {
				last;
			}
		}

		if ($dealerscore < 17) {
			$dealerinput = 1;
		} else {
			$dealerinput = 0;
		}

		if ($dealerinput == 1) {
			my $dealernext = new Card();
			$dealernext->setname($deck->draw()->getname());
			my $dealernextname = $dealernext->getname();
			my $dealernextvalue = $dealernext->getvalue();
			if ($dealernextvalue == 1) {
				$dealeraces++;
				$dealernextvalue = 11;
			} elsif ($dealernextvalue == 11 || $dealernextvalue == 12 || $dealernextvalue == 13) {
				$dealernextvalue = 10;
			}
			$dealerscore += $dealernextvalue;
		}
	}

	print "Your score is $myscore and the dealer's score is $dealerscore.\n";
	if ($myscore > 21 || ($dealerscore <= 21 && $dealerscore >= $myscore)) {
		return 0;
	} else {
		return 1;
	}
}